<?php
$dataUrl = 'http://app.hao123.com/tuangou/data/tgdata_1_0_1.js';
/*
*/
try {
	$data = file_get_contents($dataUrl);
	$data = str_ireplace("var strTgData=", "",$data);
	$data = str_ireplace(";", "",$data);
	//$data = substr_replace($data, "", 0, 4);
	//$data = explode('],"\u', $data);
	//$bj = str_ireplace('{"\u5317\u4eac":', '', $data[0]) . ']';

	//$sql = "SET NAMES utf8;";
	$sql = "";
echo "<script type='text/javascript'>var d = " . $data . ";</script>";

	$jd = json_decode($data, true);
//print_r($jd);
//var_dump(current($jd));
	foreach(current($jd) as $jsonItem)
	{
		//$jsonItems = json_decode(substr_replace($item, "", 0, 12) . "]");
		//foreach($jsonItems as $jsonItem)
		//{
			$sql = $sql . "INSERT INTO items (title, cover_image_url, content, value, price, rebate, start_time, end_time, url, website, site_url, city, user_id, created) SELECT ";
			$sql = $sql . "'" . $jsonItem['title'] . "',";
			$sql = $sql . "'" . $jsonItem['image'] . "',";
			$sql = $sql . "'" . $jsonItem['title'] . "',";
			$sql = $sql . "'" . $jsonItem['value'] . "',";
			$sql = $sql . "'" . $jsonItem['price'] . "',";
			$sql = $sql . "'" . $jsonItem['rebate'] . "',";
			$sql = $sql . "'" . $jsonItem['startTime'] . "',";
			$sql = $sql . "'" . $jsonItem['endTime'] . "',";
			$sql = $sql . "'" . $jsonItem['item_url'] . "',";
			$sql = $sql . "'" . $jsonItem['website'] . "',";
			$sql = $sql . "'" . $jsonItem['siteurl'] . "',";
			$sql = $sql . "'" . $jsonItem['city'] . "',";
			$sql = $sql . "'10000',";
			//$sql = $sql . "'0',";
			$sql = $sql . "'" . date('Y-m-d h:i:s') . "' ";
			$sql = $sql . "FROM DUAL WHERE NOT EXISTS (SELECT id FROM items WHERE url = '" . $jsonItem['item_url'] . "');";
		//}
	}
	$query = explode(";", $sql);
//	@ $db = new mysqli('localhost', 'yamone_51723', 'qv3nsZrROfMf', 'yamone_yuetaohui');
	@ $db = new mysqli('localhost', 'root', 'amomtest', 'yueth');
	if (mysqli_connect_errno()) {
		printf("Connect failed: %s\n", mysqli_connect_error());
		exit();
	}
$db->query("SET NAMES utf8");
	foreach ($query as $q)
	{
		$db->query($q);
	//	echo "<p>" . $q . "</p>";
	}
	//$result = $db->query($sql);
	$rows = $db->affected_rows;

	echo "<b>" . $rows . " records has added</b>";
	echo "<br /><span style='color: red'>" . mysqli_error($db) . "</span>";
	echo "<br />" . count($query);

	//$result->free();
	$db->close();
}
catch (Exception $e) {
	echo $e->getMessage();
}
//die();
?>
